﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Drawing.Design;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using C4F.VistaP2P.Win.Chat;
using C4F.VistaP2P.Win.File;
using C4F.VistaP2P.Win.Media;
using C4F.VistaP2P.Win.Network;
using C4F.VistaP2P.Win.Picture;


namespace C4F.VistaP2P.Win.Network
{
    public class NetworkDesignerActionList : DesignerActionList
    {

       
        /// <summary>
        /// This class enables WinForms Design Time support
        /// </summary>
        public NetworkDesignerActionList(IComponent component)
            : base(component)
        {
            this.AutoShow = true;
        }

        /// <summary>
        /// Returns the list of items to display in the smart tag.
        /// </summary>
        /// <returns>the list of items to display in the smart tag.</returns>
        public override DesignerActionItemCollection GetSortedActionItems()
        {

            // Create list to store designer action items
            DesignerActionItemCollection actionItems = new DesignerActionItemCollection();


            //// Add Design category header text
            actionItems.Add(new DesignerActionHeaderItem("P2P Controls"));

            // Add Design category descriptive label
            actionItems.Add(
              new DesignerActionTextItem(
                "Properties that connect the NetworkManager to the other Peer2Peer Controls.",
                "Design"));

            // Dock/Undock designer action method item
            actionItems.Add(
              new DesignerActionMethodItem(
                this,
                "ToggleDockStyle",
                GetDockStyleText(),
                "Design",
                "Dock or undock this control in it's parent container.",
                true));

     
            actionItems.Add(
              new DesignerActionPropertyItem(
                "Visible",
                "Visible",
                GetCategory(NetworkControl, "Visible"),
                GetDescription(NetworkControl, "Visible")));

            actionItems.Add(
                new DesignerActionPropertyItem(
                  "Audio",
                  "Audio Control",
                  GetCategory(this.NetworkControl, "Audio"),
                  GetDescription(this.NetworkControl, "Audio")));

            actionItems.Add(
              new DesignerActionPropertyItem(
                "Chat",
                "Chat Control",
                GetCategory(this.NetworkControl, "Chat"),
                GetDescription(NetworkControl, "Chat")));

            actionItems.Add(
             new DesignerActionPropertyItem(
               "File",
               "File Control",
               GetCategory(this.NetworkControl, "File"),
               GetDescription(this.NetworkControl, "File")));


            actionItems.Add(
             new DesignerActionPropertyItem(
               "Picture",
               "Picture Control",
               GetCategory(this.NetworkControl, "Picture"),
               GetDescription(this.NetworkControl, "Picture")));


            actionItems.Add(
                new DesignerActionPropertyItem(
                  "Video",
                  "Video Control",
                  GetCategory(this.NetworkControl, "Video"),
                  GetDescription(this.NetworkControl, "Video")));

            // Return action list to design time item
            return actionItems;
        }


        /// <summary>
        /// Gets or sets the chat control that the network manager should use.
        /// </summary>
        public ChatControl Chat
        {
            get { return this.NetworkControl.Chat; }
            set { SetProperty("Chat", value); }
        }

        /// <summary>
        /// Gets or sets the file control that the network manager should use.
        /// </summary>
        public FileControl File
        {
            get { return this.NetworkControl.File; }
            set { SetProperty("File", value); }
        }

        /// <summary>
        /// Gets or sets the picture control that the network manager should use.
        /// </summary>
        public PictureControl Picture
        {
            get { return this.NetworkControl.Picture; }
            set { SetProperty("Picture", value); }
        }


        /// <summary>
        /// Gets or sets the media control that the network manager should use.
        /// </summary>
        public MediaControl Audio
        {
            get { return this.NetworkControl.Audio; }
            set { SetProperty("Audio", value); }
        }

        /// <summary>
        /// Gets or sets the video control that the network manager should use.
        /// </summary>
        public MediaControl Video
        {
            get { return this.NetworkControl.Video; }
            set { SetProperty("Video", value); }
        }
        

        /// <summary>
        /// Gets or sets if the control should be visible.
        /// </summary>
        public bool Visible
        {
            get { return this.NetworkControl.ShadowVisible; }
            set { SetProperty("ShadowVisible", value); }
        }


        /// <summary>
        /// Gets or sets if this network manager should be docked.
        /// </summary>
        public void ToggleDockStyle()
        {
            // Toggle ClockControl's Dock property
            if (this.NetworkControl.Dock != DockStyle.Fill)
            {
                SetProperty("Dock", DockStyle.Fill);
            }
            else
            {
                SetProperty("Dock", DockStyle.None);
            }
        }

        /// <summary>
        ///  Helper method that returns an appropriate
        ///  display name for the Dock/Undock property,
        ///  depending on the dock property value
        /// </summary>
        /// <returns></returns>
        private string GetDockStyleText()
        {
            if (this.NetworkControl.Dock == DockStyle.Fill)
            {
                return "Undock in parent container";
            }
            else
            {
                return "Dock in parent container";
            }
        }


        /// <summary>
        /// Helper property to aquire a reference to the network control
        /// </summary>
        private NetworkManager NetworkControl
        {
            get { return (NetworkManager)this.Component; }
        }


        
        /// <summary>
        /// Helper method to safely set a component’s property
        /// </summary>
        /// <param name="propertyName">The property to set</param>
        /// <param name="value">the value to set</param>
        private void SetProperty(string propertyName, object value)
        {
            // Get property
            PropertyDescriptor property = TypeDescriptor.GetProperties(this.NetworkControl)[propertyName];
            // Set property value
            property.SetValue(this.NetworkControl, value);
        }

        /// <summary>
        /// Helper method to return the Category string from a 
        /// CategoryAttribute assigned to a property exposed by 
        /// the specified object
        /// </summary>
        /// <param name="source">the control to get the property from</param>
        /// <param name="propertyName">the property name to get</param>
        /// <returns>the property value</returns>
        private static string GetCategory(object source, string propertyName)
        {
            PropertyInfo property = source.GetType().GetProperty(propertyName);
            CategoryAttribute attribute = (CategoryAttribute)property.GetCustomAttributes(typeof(CategoryAttribute), false)[0];
            if (attribute == null) return null;
            return attribute.Category;
        }

        /// <summary>
        /// Helper method to return the Description string from a 
        /// CategoryAttribute assigned to a property exposed by 
        /// the specified object
        /// </summary>
        /// <param name="source">the control to get the property from</param>
        /// <param name="propertyName">the property name to get</param>
        /// <returns>the property value</returns>
        private static string GetDescription(object source, string propertyName)
        {
            PropertyInfo property = source.GetType().GetProperty(propertyName);
            DescriptionAttribute attribute = (DescriptionAttribute)property.GetCustomAttributes(typeof(DescriptionAttribute), false)[0];
            if (attribute == null) return null;
            return attribute.Description;
        }
    }
}
